//
//  RegisterFinishUserInfoViewController.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-9-28.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "RegisterFinishUserInfoViewController.h"

@interface RegisterFinishUserInfoViewController ()<UITextFieldDelegate>
{
    UIButton *maleButton;
    UIButton *femaleButton;
    UITextField *birthDate;
    UIButton *finishButton;

    UIDatePicker *datePicker;

    UIScrollView * mScrollView;
    
}
@end

@implementation RegisterFinishUserInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.leftBtn.hidden = NO;
    self.titleLabel.text = @"注册";
     self.contentView.backgroundColor = UIColorFromRGB(0xffffefa);
    
    mScrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
    if(iPhone5){
        mScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH, self.contentView.frame.size.height);
    }else{
        mScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH, self.contentView.frame.size.height+60);
    }
    [self.contentView addSubview:mScrollView];

    
    UILabel *youhuiInfo = [[UILabel alloc]init];
    youhuiInfo.frame = CGRectMake(18, 5, UI_SCREEN_WIDTH, 30);
    youhuiInfo.textAlignment = NSTextAlignmentLeft;
    youhuiInfo.text = @"完善宝宝信息，用来获取对应年龄的优惠卷";
    youhuiInfo.textColor = UIColorFromRGB(0xb6b6b4);
    youhuiInfo.font = [UIFont systemFontOfSize:14.0f];
    [mScrollView addSubview:youhuiInfo];
    
    UILabel *babySex = [[UILabel alloc]init];
    babySex.frame = CGRectMake(18, height_y(youhuiInfo)+10, 60, 30);
    babySex.text = @"宝宝性别";
    babySex.font = [UIFont systemFontOfSize:15.0f];
    babySex.textColor = UIColorFromRGB(0x666666);
    [mScrollView addSubview:babySex];
    
    UIView *chooseView = [[UIView alloc]initWithFrame:CGRectMake(90,height_y(youhuiInfo)+10, 220, 120)];
    chooseView.backgroundColor = [UIColor clearColor];
    [mScrollView addSubview:chooseView];
    
    maleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    maleButton.frame = CGRectMake(0, 10, 80, 80);
    maleButton.backgroundColor = [UIColor clearColor];
    [maleButton addTarget:self action:@selector(maleChoseButton:) forControlEvents:UIControlEventTouchUpInside];
    maleButton.tag = 101;
    [maleButton setImage:[UIImage imageNamed:@"boy"] forState:UIControlStateNormal];
    [maleButton setImage:[UIImage imageNamed:@"boy_selected"] forState:UIControlStateSelected];
    [chooseView addSubview:maleButton];
    
    femaleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    femaleButton.frame = CGRectMake(chooseView.frame.size.width-80-10, 10, 80, 80);
    femaleButton.backgroundColor = [UIColor clearColor];
    [femaleButton addTarget:self action:@selector(maleChoseButton:) forControlEvents:UIControlEventTouchUpInside];
    femaleButton.tag = 102;
    [femaleButton setImage:[UIImage imageNamed:@"girl"] forState:UIControlStateNormal];
    [femaleButton setImage:[UIImage imageNamed:@"girl_selected"] forState:UIControlStateSelected];
    [chooseView addSubview:femaleButton];

    
    
    UILabel *babyAge = [[UILabel alloc]init];
    babyAge.frame = CGRectMake(18, height_y(chooseView)+5, 60, 30);
    babyAge.text = @"宝宝年龄";
    babyAge.font = [UIFont systemFontOfSize:15.0f];
    babyAge.textColor = UIColorFromRGB(0x666666);
    [mScrollView addSubview:babyAge];
    
    birthDate = [[UITextField alloc]init];
    birthDate.borderStyle = UITextBorderStyleRoundedRect;
    birthDate.frame = CGRectMake(18+60+10,height_y(chooseView),(UI_SCREEN_WIDTH - 20*2-60-10), 40);
    birthDate.placeholder = @"请选择宝宝生日";
    birthDate.delegate = self;
    birthDate.textAlignment = NSTextAlignmentLeft;
    [mScrollView addSubview:birthDate];
    
    
    finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame = CGRectMake(15, height_y(birthDate)+20, (UI_SCREEN_WIDTH - 2*15), 40);
    [finishButton setBackgroundImage:LOADIMAGE(@"pressedBg@2x") forState:UIControlStateNormal];
    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(finishButtonPressed) forControlEvents:UIControlEventTouchUpInside];
     [finishButton setBackgroundImage:[UIImage imageNamed:@"pressedBg"] forState:UIControlStateHighlighted];
    
    finishButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [mScrollView addSubview:finishButton];
    
    datePicker = [[UIDatePicker alloc]init];
    datePicker.frame = CGRectMake(0, UI_SCREEN_HEIGHT, UI_SCREEN_WIDTH, 260);
    datePicker.datePickerMode = UIDatePickerModeDate;
//    NSDateComponents *comp = [[NSDateComponents alloc]init];
//    [comp setMonth:01];
//    [comp setDay:01];
//    [comp setYear:2010];
//    NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
//    NSDate *minDate = [myCal dateFromComponents:comp];
//    NSDateComponents *comptwo = [[NSDateComponents alloc]init];
//    [comptwo setMonth:12];
//    [comptwo setDay:31];
//    [comptwo setYear:2016];
//    NSCalendar *myCalTwo = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
//    NSDate *maxDate = [myCalTwo dateFromComponents:comptwo];
//    datePicker.maximumDate = maxDate;
//    datePicker.minimumDate = minDate;
    [datePicker addTarget:self action:@selector(datePickerValueChange) forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:datePicker];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)maleChoseButton:(id)sender
{
    [self pushBackPickerView];
    UIButton *senderbtn = (UIButton *)sender;
    if (senderbtn.tag == 101) {
        maleButton.selected = YES;
        femaleButton.selected = NO;
        sex = @"M";
    }else if (senderbtn.tag == 102){
        femaleButton.selected = YES;
        maleButton.selected = NO;
        sex = @"F";
    }
}

-(void)finishButtonPressed
{
    finishButton.enabled = NO;
    [self pushBackPickerView];
    [self sendRegisterMessageToServer];
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [birthDate resignFirstResponder];
    [UIView animateWithDuration:.5 animations:^{
        datePicker.frame = CGRectMake(0, UI_SCREEN_HEIGHT-260, UI_SCREEN_WIDTH, 260);
        if (iPhone5){
        
        }else{
            mScrollView.contentOffset = CGPointMake(0, 60);        
        }

    }];
    [self getDateString];

}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self pushBackPickerView];
}
-(void)pushBackPickerView
{
    [UIView animateWithDuration:.5 animations:^{
        datePicker.frame = CGRectMake(0, UI_SCREEN_HEIGHT, UI_SCREEN_WIDTH, 260);
    }];
}

-(void)datePickerValueChange
{
    [self getDateString];
}
-(void)getDateString
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSString *  locationString = [dateFormat stringFromDate:datePicker.date];
    birthDate.text = locationString;
}
-(void)sendRegisterMessageToServer
{
    //http://123.57.16.119/index.php?m=Api&c=Customer&a=getCustomerOrder&CustomerID=''
    //http://123.57.16.119/index.php?m=Api&c=Index&a=sendsms

    if(![XDTools NetworkReachable]){
        finishButton.enabled = YES;
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    if (!IS_NOT_EMPTY(sex)) {
        finishButton.enabled = YES;
        [XDTools showTips:@"请选择宝宝性别" toView:self.contentView];
        return;
    }
    if (!IS_NOT_EMPTY(birthDate.text)) {
        finishButton.enabled = YES;
        [XDTools showTips:@"请选择宝宝生日" toView:self.contentView];
        return;
    }

    NSDictionary *requestDic = @{@"MobilePhone":_phoneNumber,@"LoginPwd":_passwordString,@"Code":_verifyCode,@"BabyGend":sex,@"BabyBirthday":birthDate.text};
    ASIHTTPRequest *request = [XDTools postRequestWithDict:requestDic API:API_REGISTER];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        NSDictionary *resultDic = [XDTools JSonFromString:[mrequest responseString]];
        DDLOG(@"res....%@",resultDic);
        if ([[resultDic objectForKey:@"status"] intValue]==1) {
         
            [self initUserInfo:resultDic];
//            [self finishButtonPressed];
            [XDTools showTips:@"注册成功" toView:self.contentView];
            [self performSelector:@selector(delyaToPush) withObject:nil afterDelay:.8f];

        }else{
            finishButton.enabled = YES;
            [XDTools showTips:[resultDic objectForKey:@"msg"] toView:self.contentView];
        }

    }];
    [request setFailedBlock:^{
        finishButton.enabled = YES;
        [XDTools hideProgress:self.contentView];
        if (mrequest.error.code == 2) {
            [XDTools showTips:@"网络请求超时" toView:self.view];
        }
    }];
    [request setTimeOutSeconds:12];
    [request startAsynchronous];

}
-(void)initUserInfo:(NSDictionary *)userInfo
{
    
    if ([[userInfo objectForKey:@"data"] objectForKey:@"TOKEN"] && [[userInfo objectForKey:@"data"] objectForKey:@"UID"]) {
        [[NSUserDefaults standardUserDefaults]setObject:[[userInfo objectForKey:@"data"] objectForKey:@"UID"] forKey:kMY_USER_ID];
        [[NSUserDefaults standardUserDefaults]setObject:[[userInfo objectForKey:@"data"] objectForKey:@"TOKEN"] forKey:kMY_USER_TOKEN];
        [[NSUserDefaults standardUserDefaults] setObject:_phoneNumber forKey:kMY_USER_IPHONE];
        [[NSUserDefaults standardUserDefaults] setObject:_phoneNumber forKey:kMY_USER_NAME];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [XDTools showTips:@"服务器参数错误" toView:self.contentView];
    }
}
-(void)delyaToPush
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}





@end
